package com.umeng.android.utils;

import java.io.File;


import android.R.integer;
import android.os.Message;
import cn.kuaipan.android.sdk.KPManager;
import cn.kuaipan.android.sdk.activity.MainActivity;
import cn.kuaipan.android.sdk.net.KPCallback;
import cn.kuaipan.android.sdk.net.KPException;

/** 
 * @author liubin 
 * @date：Mar 9, 2013 10:43:42 AM  
 */

public class KuaiPanManager {

	/**
	 * obtain user information
	 */
	public void getUserInfo(){
		KPManager.getOpenAPI().accountInfo(new KPManagerListener(Constants.USER_INFO));
	}
	
	/**
	 * user login
	 */
	public void login(){
		
		
	}
	
	/**
	 * obtain file or folder information
	 * @param path
	 */
	public void getFolderInfo(String path){ 
		
		KPManager.getOpenAPI().metadata(path, true, new KPManagerListener(Constants.USER_FOLDER));
		
	}
	
	/**
	 * create a folder 
	 * @param path 
	 */
	public void createFolder(String path){
		
		KPManager.getOpenAPI().createFolder(path, new KPManagerListener(Constants.USER_CREATE_FOLDER));
		
	}
	
	/**
	 * move folder or a file
	 * @param srcPath
	 * @param destPath
	 */
	public void moveFolder(String srcPath,String destPath){
		
		KPManager.getOpenAPI().move(srcPath, destPath, new KPManagerListener(Constants.USER_MOVE_FOLDER));
		
	}
	
	/**
	 * copy a folder or a file
	 * @param srcPath
	 * @param destPath
	 */
	public void copyFolder(String srcPath,String destPath){
		
		KPManager.getOpenAPI().copy(srcPath, destPath, new KPManagerListener(Constants.USER_COPY_FOLDER));
		
	}
	
	/**
	 * delete a folder or a file
	 * @param filePath
	 */
	public void deleteFolder(String filePath){
		
		KPManager.getOpenAPI().delete(filePath, new KPManagerListener(Constants.USER_DELETE_FOLDER));
		
	}
	
	/**
	 * upload a file
	 * @param director
	 * @param file
	 */
	public void uploadFile(String director,File file){
		
		KPManager.getOpenAPI().upload(director, file, new KPManagerListener(Constants.USER_UPLOAD_FILE));
		
	}
	
	/**
	 * download a file 
	 * @param filePath
	 * @param savaPath
	 */
	public void downloadFile(String filePath,String savaPath){
		
		KPManager.getOpenAPI().download(filePath, new File(savaPath), false, new KPManagerListener(Constants.USER_DOWNLOAD_FILE));
		
	}
	
	public void getFileThumbnail(String filePath,int width,int height){
		
		KPManager.getOpenAPI().thumbnail(width, height, filePath, new KPManagerListener(Constants.USER_FILE_THUMBNAIL));
		
	}
	/**
	 * obtain share link
	 * @param path
	 */
	public void getShareLink(String path){ 
		
		KPManager.getOpenAPI().shares(path, null, null, new KPManagerListener(Constants.USER_SHARE_LINK));
		
	}
	
	/**
	 * send message to MainActivity
	 * @param message
	 */
	public static void sendMessage(Message message){
		
		MainActivity.handler.sendMessage(message);
		
	}

	private class KPManagerListener implements KPCallback{
		
		Message message = null;
		
		public KPManagerListener(int messageType){
			message = new Message();
			message.arg2=messageType;
		} 
		@Override
		public void onFail(int arg0, KPException arg1, String arg2) {
			
			message.arg1=Constants.FAIL;
			message.obj = arg1;
			sendMessage(message);
			
		}

		@Override
		public void onProgress(long arg0, long arg1) {
			
		}

		@Override
		public void onSuccess(Object arg0) {
			
			message.arg1=Constants.SUCCESS;
			message.obj=arg0;
			sendMessage(message);
			
		}
	}
}
